<?php
/**
 * @author zhaofei
 * @package 终端管理 
 * @createtime 2014-11-27
 * @updatetime 2014-11-27
 * @ps:暂为最高权限用户
 */
namespace System\Controller;
use Think\Controller;
class StoreController extends Controller {
	function __construct(){
		parent::__construct();
		$ranks = session('ranks');
		if($ranks!=1){
			$this->error('对不起你没有权限',U('Login/index'));
		}
	}

    public function index(){
        $model = M('Store'); // 实例化Store对象
        $count = $model->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function add(){
        
        if(I('get.id')){
             $Store = D('Store')->where(array('id'=>I('get.id')))->find();
             $this->assign('Store',$Store);
             $currents = D('Store_class')->where(array('id'=>$Store['p_id']))->find();
             $this->assign('currents',$currents);

        }
        $this->assign('data',$data);
        $this->display();
    }

    public function addStore(){
        $data = D('Store')->create();
        if($data==false){
            $this->error(D('Store')->getError());
        }
        if(!I('post.id')){
            $rs = D('Store')->add($data);
        }else{
            $rs = D('Store')->where(array('id'=>I('post.id')))->save($data);  
        }

        if($rs){
            $this->success('操作成功',U('index'));
        }else{
            $this->error('系统故障');
        }
    }

     public function del(){
        $id = I('get.id');
        if(!$id){
            $this->error('参数错误');
        }
        $rs = D('Store')->delete($id);
        if($rs){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('系统故障');
        }
    }

  


    //终端导入
    public function importExcel(){
        $return=$this->localUpload(array('xls'));
        if ($return['error']){
            $this->error($return['msg']);
        }else {
            vendor('Spreadsheet');
            $data = new \Spreadsheet_Excel_Reader();
            // 设置输入编码 UTF-8/GB2312/CP936等等
            $data->setOutputEncoding('UTF-8');
            $filename = str_replace('http://'.$_SERVER['HTTP_HOST'],$_SERVER['DOCUMENT_ROOT'],$return['msg']);
            $data->read($filename);
            chmod(str_replace('http://'.$_SERVER['HTTP_HOST'],$_SERVER['DOCUMENT_ROOT'],$return['msg']),0777);
            //
            chmod($filename,0777);
            //
            $sheet=$data->sheets[0];
            $rows=$sheet['cells'];
         
            if ($rows){
                $i=0;
                $num=0;
                $time = time();
                foreach ($rows as $r){
                    if ($i!=0){
                        $Two = array();
                        $db=M('Store');
                        $data = array(
                            'provice'=>trim($r[1]),
                            'city'=>trim($r[2]),
                            'area'=>trim($r[3]),
                            'name'=>trim($r[4]),
                            'status'=>1,
                            'sort'=>0,
                            'create_time'=>time(),
                            'ct_by_id'=>session('id'),
                            'up_by_id'=>session('id'),
                            'update_time'=>time(),
                        );
                      //  echo D('StoreClass')->getLastSql();

                        $check=$db->where(array('name'=>$data['name']))->find();
                        if($data['name'] && $data['provice'] && $data['city'] && !$check){
                            $db->add($data);
                            $num++;
                        } 
                    }
                    $i++;
                }
            }
             $this->success('导入终端'.$num.'条',U('index'));
        }
    }


    function localUpload($filetypes=''){
        $config = array(
            'maxSize' => 1024*100,
            'rootPath' => './Uploads/',
            'savePath' => '',
            'saveName' => array('uniqid',''),
            'exts' => $filetypes,
            'autoSub' => true,
            'subName' => array('date','Ymd'),
        );

        $upload = new \Think\Upload($config);
        if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/Uploads') || !is_dir($_SERVER['DOCUMENT_ROOT'].'/Uploads')){
            mkdir($_SERVER['DOCUMENT_ROOT'].'/Uploads',0777);
        }
        $info = $upload->uploadOne($_FILES['name']);

        if(!$info) {// 上传错误提示错误信息
            $error=1;
            $msg=$upload->getError();
        }else{// 上传成功 获取上传文件信息
            $error=0;
            $msg= 'http://'.$_SERVER['HTTP_HOST'].'/Uploads/'.$info['savepath'].$info['savename'];
        }
        return array('error'=>$error,'msg'=>$msg);
    }
     
    



}